import { Component, OnInit } from '@angular/core';
import { MerchantcashrecordService } from '../../service/merchantcashrecord.service';
import { MerchantinfoService } from '../../service/merchantinfo.service';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from 'src/app/service/common.service';
import { ComponentBasic } from '../../app.component';
import { RoleRight } from '../../../kalend';
import { Router, ActivatedRoute } from '@angular/router';
import { PlatformuserService } from '../../service/platformuser.service';
import { PaysetService } from '../../service/payset.service';
@Component({
  selector: 'app-merchantinfo',
  templateUrl: './merchantinfo.component.html',
  styleUrls: ['./merchantinfo.component.scss'],
  providers: [PlatformuserService, MerchantcashrecordService, CommonService, MerchantinfoService,PaysetService]
})
export class MerchantinfoComponent extends ComponentBasic implements OnInit {
  loading = true;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  doc: any = {
    type: '餐费账户'
  };
  sktype="";
  dataSet = [];
  sdate: any;
  edate: any;
  iscontact: any;
  remarks: any;
  //提现金额
  cashmoney: any;
  isVisible = false;
  isEditVisible = false;
  isOkLoading = false;
  LoginUser = RoleRight.getStorage();
  statuslist = [{ key: null, value: '全部' }, { key: '1', value: '已联系' }, { key: '0', value: '未联系' }];
  typelist = [{ key: '餐费账户', value: '餐费账户' }, { key: '招募账户', value: '招募账户' }, { key: '餐币账户', value: '餐币账户' }];
  sktypelist=[{ key: '微信零钱', value: 'wechat' }, { key: '银行卡', value: 'bank' }];
  sk:any;
  skopenid:any;
  skuserlist:any;
  constructor(private paysetService: PaysetService,private userservice: PlatformuserService, private merchantinfoService: MerchantinfoService, private router: Router, private merchantcashrecord: MerchantcashrecordService, private msg: NzMessageService, private commonservice: CommonService) {
    super();
    super.setLayout(true, true);
  }

  ngOnInit() {

    this.searchData();
  }
  searchData(reset: boolean = false): void {
    this.loading = true;
    this.merchantinfoService.querybyuid({
      offset: this.pageIndex,
      limit: this.pageSize,
      userid: this.LoginUser._id
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }

  edit(id): void {
    this.isEditVisible = true;
    this.merchantinfoService.queryone({ _id: id }).then((res) => {
      this.doc = res;
    });
  }

  crsh(id): void {
    this.isVisible = true;
    this.merchantinfoService.queryone({ _id: id }).then((res) => {
      this.doc = res;
      this.doc.money = res.balance;
    });

    this.paysetService.query({
      merid: id,
    }).then((res) => {
      this.skuserlist=res;
        this.sk = res.map((item) => {
        
        return {
          key: item.wechatrealname+'|'+item.wechatphone+'|'+item.merchantname,
          value: item.wechatopenid
        };
     
    });

  });}

  settlerecords(id): void {
    this.router.navigate(['salesmanage/settlementrecordsMid/' + id]);
  }


  inv(id): void {
    this.userservice.queryById(this.LoginUser._id).then((res) => {
      if (res.customerid) {
        this.router.navigate(['sysset/inverhistory/' + res.customerid]);
      } else {
        this.msg.error('请联系管理员关联您的小程序账户');
      }

    });

  }
  // 确认编辑修改完毕
  handleEditOk(): void {


  }


  // 确认提现完毕
  handleOk(): void {
    if(this.sktype==""){
      this.msg.warning('请选择收款方式');
      return;
    }
    if (parseFloat(this.cashmoney).toString() === 'NaN') {
      this.msg.warning('请输入正确提现金额！');
      return;
    }

    if (!this.skopenid) {
      this.msg.warning('请选择微信收款账户');
      return;
    }
    if (Number(this.cashmoney) < 10) {
      this.msg.warning('提现金额不得低于10元！');
      return;
    }
    if (this.doc.money < this.cashmoney) {
      this.msg.warning('提现金额不能大于余额！');
      return;
    }


 
    this.skuserlist.map((item)=>{
      if(item.wechatopenid==this.skopenid){
        this.doc.wechatphone=item.wechatphone;
        this.doc.userrealname=item.wechatrealname;
        this.doc.useridno=item.useridno;
      }
    });

    const parm = {
      totalcase: this.cashmoney,
      BankCardNo:this.sktype=='wechat'?'/': this.doc.BankCardNumber,
      merchantid: this.doc._id,
      Bank:this.sktype=='wechat'?'/': this.doc.Bank,
      merchantname: this.doc.name,
      type: this.doc.type,
      cashchannel:this.sktype,
      wechatopenid:this.skopenid,
      wechatphone:this.doc.wechatphone,
      userrealname:this.doc.userrealname,
      useridno:this.doc.useridno
    }
    console.log(parm)
    this.merchantcashrecord.create(parm).then((res) => {
      this.searchData();
      this.isVisible = false;
      this.doc = {};
      this.msg.warning(res);
    });

  }

  handleCancel(): void {
    this.isVisible = false;
    this.doc = {
      type: '餐费账户'
    };
  }

  edithandleCancel(): void {
    this.isEditVisible = false;
  }

  change() {
    if (this.doc.type === '餐费账户') {
      this.doc.money = this.doc.balance;
    } else if (this.doc.type === '招募账户') {
      this.doc.money = this.doc.balanceviter;
    } else if (this.doc.type === '餐币账户') {
      this.doc.money = this.doc.mealbalance;
    }
  }

  skchange(){

  }
}
